var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var ad = require("./ad");
var point_map = require("./pointMap");

var shop = {
  is_over_today: false,
  go_page_from_home() {
    var logger = utils.create_logger("shop go_page_from_home: ");
    logger("");
    utils.click_point.apply(
      utils,
      utils.get_size_point(point_map["首页赚钱"], 1080, 2340)
    );
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    if (this.is_over_today) {
      logger("今天的逛街任务已全部执行完毕");
      return;
    }
    this.init();
  },
  init() {
    var logger = utils.create_logger("shop init: ");
    logger("");
    var ocr_text = ocr_utils.find_text("去逛街", false);
    if (ocr_text) {
      logger("找到-去逛街-");
      utils.click_ocr_text(ocr_text);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
      var ui_text = utils.exists_text_matches(/浏览\d+秒可领\d+金币/);
      if (ui_text) {
        logger("找到-" + ui_text.text() + "-");
        var interval = utils.get_number(ui_text.text());
        this.run(interval);
        return;
      }
      var ui_text_end = utils.exists_text_starts_with("明日浏览");
      if (ui_text_end) {
        logger("找到-" + ui_text_end.text() + "-,今天的逛街任务已全部执行完毕");
        this.is_over_today = true;
        this.after();
        return;
      }
      logger("没找到-" + `/浏览\d+秒可领\d+金币/` + "-");
      var ocr_text = ocr_utils.screen_exists_text_one("去逛街", false);
      if (ocr_text) {
        logger("找到-去逛街-");
        this.next();
        return;
      }
      logger("没找到-去逛街-");
      this.after();
      return;
    }
    logger("没找到-去逛街-");
    this.next();
  },
  run(interval) {
    var logger = utils.create_logger("shop run: ");
    interval = interval || 60;
    logger("将运行-" + interval + "-秒");
    var has_watch_interval = 0;
    var up_times = 0;
    var down_times = 0;
    var max_up_times = utils.get_random_interval(2, 0, 1, 1);
    while (has_watch_interval < interval) {
      if (up_times < max_up_times) {
        swipe(
          (device.width / 2) * utils.get_swipe_times(95, 5),
          (device.height / 10) * 8 * utils.get_swipe_times(95, 5),
          (device.width / 2) * utils.get_swipe_times(95, 5),
          (device.height / 10) * 2 * utils.get_swipe_times(95, 5),
          utils.get_random_interval(500, 0, 500, 1)
        );
        up_times = up_times + 1;
        logger("已经向上滑动-1-次");
      } else {
        var random_num = random(0, 1);
        if (random_num == 0 && down_times < up_times) {
          //向下滑动
          utils.swipe_x(
            device.width / 2,
            (device.height / 10) * 5,
            device.width / 2,
            (device.height / 10) * 8,
            utils.get_random_interval(300, 0, 700, 1)
          );
          down_times = down_times - 1;
          up_times = up_times + 1;
          logger("已经向上滑动-1-次");
          down_times = down_times + 1;
          up_times = up_times - 1;
          logger("已经向下滑动-1-次");
        } else {
          utils.swipe_x(
            device.width / 2,
            (device.height / 10) * 8,
            device.width / 2,
            (device.height / 10) * 5,
            utils.get_random_interval(300, 0, 700, 1)
          );
          down_times = down_times - 1;
          up_times = up_times + 1;
          logger("已经向上滑动-1-次");
        }
      }
      var this_watch_interval = utils.get_random_interval(2, 0, 1, 1000);
      sleep(this_watch_interval);
      has_watch_interval =
        has_watch_interval + this_watch_interval / 1000 + 0.2;
      logger("已运行-" + has_watch_interval + "-秒");
    }
    this.after();
  },
  after() {
    console.log("shop after");
    back();
    sleep(utils.get_random_interval(1, 0, 1, 1000));
    this.next();
  },
  next() {
    console.log("shop next");
  },
};
module.exports = shop;
